package admin

import (
	"github.com/labstack/echo"
	"github.com/sirupsen/logrus"
	"net/http"
	"src/config"
	"src/controller/param"
	"src/model"
	"src/util/context"
	"src/util/log"
)

var adminhotkeyLogger = log.GetLogger()

func writeadminhotkeyLoggerLoggerLog(funcName, errMsg string, err error) {
	adminhotkeyLogger.WithFields(logrus.Fields{
		"package":  config.LogMessage + "/admin",
		"file":     "hotkey.go",
		"function": funcName,
		"err":      err,
	}).Warn(errMsg)
}

func AddHotkey(c echo.Context) error {
	Key := param.Hotkey{}
	err := c.Bind(&Key)
	if err != nil {
		writeadminhotkeyLoggerLoggerLog("AddHotkey", config.ErrorMsgParamWrong, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorMsgParamWrong)
	}
	err = model.AddHotMem(Key.Key)
	if err != nil {
		writeadminhotkeyLoggerLoggerLog("AddHotkey", "AddError", err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, "error:"+err.Error())
	}
	return context.RetData(c, "")
}
func DeleteHotkey(c echo.Context) error {
	Key := param.Hotkey{}
	err := c.Bind(&Key)
	if err != nil {
		writeadminhotkeyLoggerLoggerLog("DeleteHotkey", config.ErrorMsgParamWrong, err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, config.ErrorMsgParamWrong)
	}
	err = model.DeleteHotkey(Key.Key)
	if err != nil {
		writeadminhotkeyLoggerLoggerLog("DeleteHotkey", "DeleteError", err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, "error:"+err.Error())
	}
	return context.RetData(c, "")
}
func ClearHotkey(c echo.Context) error {
	err := model.ClearHotKey()
	if err != nil {
		writeadminhotkeyLoggerLoggerLog("ClearHotkey", "ClearError", err)
		return context.RetError(c, http.StatusBadRequest, http.StatusBadRequest, "error:"+err.Error())
	}
	return context.RetData(c, "")
}
